﻿using System.Windows;
using System.Windows.Controls;
using KinonekoSoftware.CodariaDB.Presets;
using KinonekoSoftware.CodariaDB.Runtimes;
using KinonekoSoftware.Foundation.Styles;
using KinonekoSoftware.Foundation.UI;
using KinonekoSoftware.Foundation.UI.ViewModels;
using KinonekoSoftware.Studio.Models.Shells;
using NLog;

namespace KinonekoSoftware.Studio.Views.Settings
{
    public partial class RegularSettingView : UserControl
    {
        private int _count;

        public RegularSettingView()
        {
            InitializeComponent();
            Loaded += OnLoaded;
        }

        private SettingViewModel ViewModel() => DataContext as SettingViewModel;

        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            Initialize_Theme();
            Initialize_DataMode();
            Initialize_Languages();
            Initialize_AutoSave();
            Initialize_IconPresets();
            Initialize_AvatarPresets();
        }

        private void Initialize_Theme()
        {
            var source = new ObservableCollection<Theme>
            {
                Theme.Dark,
                Theme.Light,
            };
            var vm = ViewModel();
            Theme_ComboBox.ItemsSource      =  source;
            Theme_ComboBox.SelectedIndex    =  source.IndexOf(vm.Setting_UI.Theme);
            Theme_ComboBox.SelectionChanged += OnThemeChanged;
        }

        private void Initialize_DataMode()
        {
            var source = new ObservableCollection<DataMode>
            {
                DataMode.Protected,
                DataMode.Release,
            };
            var vm = ViewModel();
            DataMode_ComboBox.ItemsSource      =  source;
            DataMode_ComboBox.SelectedIndex    =  source.IndexOf(vm.Setting_App.DataMode);
            DataMode_ComboBox.SelectionChanged += OnDataModeChanged;
        }


        private void Initialize_Languages()
        {
            var source = new ObservableCollection<LanguageArea>
            {
                LanguageArea.Chinese,
                LanguageArea.English,
                LanguageArea.French,
                LanguageArea.Germany,
                LanguageArea.Japanese,
                LanguageArea.Korean,
                LanguageArea.Russian,
            };
            var vm = ViewModel();
            Language_ComboBox.ItemsSource      =  source;
            Language_ComboBox.SelectedIndex    =  source.IndexOf(vm.Setting_UI.Language);
            Language_ComboBox.SelectionChanged += OnLanguageChanged;
        }


        private void Initialize_AutoSave()
        {
            var source = new ObservableCollection<AutoSavePeriod>
            {
                AutoSavePeriod.Minute_5,
                AutoSavePeriod.Minute_10,
                AutoSavePeriod.Minute_15,
                AutoSavePeriod.Minute_20,
                AutoSavePeriod.Minute_30,
            };
            
            
            var vm   = ViewModel();
            var auto = vm.Setting_App.AutoSave;

            if (auto == AutoSavePeriod.Minute_1 ||
                auto == AutoSavePeriod.Minute_2 ||
                auto == AutoSavePeriod.Minute_3)
            {
                //
                // 修复旧版设定，自动保存时间最少为5分钟，不然过于频繁的自动保存会让体验非常糟糕。
                auto = vm.Setting_App.AutoSave = AutoSavePeriod.Minute_5;
                Platform.SaveSetting(vm.Setting_App);
            }
            
            AutoSavePeriod_ComboBox.ItemsSource      =  source;
            AutoSavePeriod_ComboBox.SelectedIndex    =  source.IndexOf(auto);
            AutoSavePeriod_ComboBox.SelectionChanged += OnAutoSaveChanged;
        }


        private void Initialize_IconPresets()
        {
            var source = new ObservableCollection<Preset_Icon>
            {
                Preset_Icon.Icon_1,
                Preset_Icon.Icon_2,
                Preset_Icon.Icon_3,
                Preset_Icon.Icon_4,
                Preset_Icon.Icon_5,
                Preset_Icon.Icon_6,
                Preset_Icon.Icon_7,
                Preset_Icon.Icon_8,
                Preset_Icon.Icon_9,
                Preset_Icon.Icon_10,
                Preset_Icon.Icon_11,
                Preset_Icon.Icon_12,
                Preset_Icon.Override,
            };
            var vm = ViewModel();
            Preset_Icon_ComboBox.ItemsSource      =  source;
            Preset_Icon_ComboBox.SelectedIndex    =  source.IndexOf(vm.Icon_Preset);
            vm.Icon                               =  vm.GetIcon(vm.Icon_Preset);
            Preset_Icon_ComboBox.SelectionChanged += OnIconPresetChanged;
            Presets_Icon_Button.Click             += OnButtonClick_PresetIcon;
            UpdateButtonState();
        }

        private void Initialize_AvatarPresets()
        {
            var source = new ObservableCollection<Preset_Avatar>
            {
                Preset_Avatar.Avatar_1,
                Preset_Avatar.Avatar_2,
                Preset_Avatar.Avatar_3,
                Preset_Avatar.Avatar_4,
                Preset_Avatar.Override,
            };
            var vm = ViewModel();
            Preset_Avatar_ComboBox.ItemsSource      =  source;
            Preset_Avatar_ComboBox.SelectedIndex    =  source.IndexOf(vm.Avatar_Preset);
            vm.Avatar                               =  vm.GetAvatar(vm.Avatar_Preset);
            Preset_Avatar_ComboBox.SelectionChanged += OnAvatarPresetChanged;
            Presets_Avatar_Button.Click             += OnButtonClick_PresetAvatar;
            UpdateButtonState();
        }



        private void UpdateButtonState()
        {
            var vm = ViewModel();

            Presets_Icon_Button.Visibility   = vm.Icon_Preset == Preset_Icon.Override ? Visibility.Visible : Visibility.Collapsed;
            Presets_Avatar_Button.Visibility = vm.Avatar_Preset == Preset_Avatar.Override ? Visibility.Visible : Visibility.Collapsed;
        }



        private async Task PushNotification()
        {
            if (_count < 2)
            {
                await ViewModelBase.Success(TitleSR.SuccessOperation, TextSR.Setting_Changed);
            }

            _count++;
        }


        private async void OnButtonClick_PresetIcon(object sender, RoutedEventArgs e)
        {
            // var vm = ViewModel();
            // var r  = await Platform.Icon();
            //
            // if (!r.IsFinished)
            // {
            //     return;
            // }
            //
            // if (vm.Icon_Preset != Preset_Icon.Override)
            // {
            //     var source = (ObservableCollection<Preset_Icon>)Preset_Icon_ComboBox.ItemsSource;
            //     Preset_Icon_ComboBox.SelectedIndex = source.IndexOf(Preset_Icon.Override);
            // }
            //
            // vm.Icon = r.Value.Image;
            // var buffer = r.Value.ImageBuffer;
            // await PresetManager.OverrideIcon(vm.Repository.Workspace, buffer);
            //
            // //
            // //
            // await PushNotification();
        }

        private async void OnButtonClick_PresetAvatar(object sender, RoutedEventArgs e)
        {
            //
            // var vm = ViewModel();
            // var r  = await Platform.Icon();
            //
            // if (!r.IsFinished)
            // {
            //     await ViewModelBase.Warning(TitleSR.WarningOperation, r.Reason);
            //     e.Handled = true;
            //     return;
            // }
            //
            // if (vm.Avatar_Preset != Preset_Avatar.Override)
            // {
            //     var source = (ObservableCollection<Preset_Avatar>)Preset_Avatar_ComboBox.ItemsSource;
            //     Preset_Avatar_ComboBox.SelectedIndex = source.IndexOf(Preset_Avatar.Override);
            // }
            //
            // vm.Avatar = r.Value.Image;
            // var buffer = r.Value.ImageBuffer;
            // await PresetManager.OverrideAvatar(vm.Repository.Workspace, buffer);
            //
            // //
            // //
            // await PushNotification();
            // e.Handled = true;
        }

        private async void OnThemeChanged(object sender, SelectionChangedEventArgs e)
        {
            var vm     = ViewModel();

            if (Theme_ComboBox.SelectedItem is Theme newTheme && newTheme != vm.Setting_UI.Theme)
            {
                //
                //
                vm.Setting_UI.Theme = newTheme;

                //
                //
                Xplat.Debug($"用户修改了主题，新主题为:{newTheme}");

                //
                //
                await Platform.SaveSetting(vm.Setting_UI);

                //
                //
                await PushNotification();
            }
        }

        private async void OnLanguageChanged(object sender, SelectionChangedEventArgs e)
        {
            var vm     = ViewModel();

            if (Language_ComboBox.SelectedItem is LanguageArea newLang && newLang != vm.Setting_UI.Language)
            {
                //
                //
                vm.Setting_UI.Language = newLang;

                //
                //
                Xplat.Debug($"用户修改了语言版本，新的语言为:{newLang}");

                //
                //
                await Platform.SaveSetting(vm.Setting_UI);

                //
                //
                await PushNotification();
            }
        }

        private async void OnDataModeChanged(object sender, SelectionChangedEventArgs e)
        {
            var vm     = ViewModel();

            if (DataMode_ComboBox.SelectedItem is DataMode newMode && newMode != vm.Setting_App.DataMode)
            {
                //
                //
                vm.Setting_App.DataMode = newMode;

                //
                //
                Xplat.Debug($"用户修改了数据模式，新模式为:{newMode}");

                //
                //
                await Platform.SaveSetting(vm.Setting_App);

                //
                //
                await PushNotification();
            }
        }

        private async void OnAutoSaveChanged(object sender, SelectionChangedEventArgs e)
        {
            var vm     = ViewModel();

            if (AutoSavePeriod_ComboBox.SelectedItem is AutoSavePeriod newValue && newValue != vm.Setting_App.AutoSave)
            {
                //
                //
                vm.Setting_App.AutoSave = newValue;

                //
                // 更新
                Ioc.Get<AppViewModel>().UpdateTimeServiceConfiguration();

                //
                //
                Xplat.Debug($"用户修改了自动保存时间，新值为:{newValue}");

                //
                //
                await Platform.SaveSetting(vm.Setting_App);

                //
                //
                await PushNotification();
            }
        }

        private async void OnIconPresetChanged(object sender, SelectionChangedEventArgs e)
        {
            var vm     = ViewModel();

            if (Preset_Icon_ComboBox.SelectedItem is Preset_Icon newValue && newValue != vm.Icon_Preset)
            {

                //
                //
                vm.Icon_Preset = newValue;
                vm.Icon        = vm.GetIcon(newValue);

                //
                //
                Xplat.Debug($"用户修改了默认的图标，新值为:{newValue}");

                //
                //
                await Platform.SaveSetting(vm.Setting_App);

                UpdateButtonState();
            }
        }

        private async void OnAvatarPresetChanged(object sender, SelectionChangedEventArgs e)
        {
            var vm     = ViewModel();

            if (Preset_Avatar_ComboBox.SelectedItem is Preset_Avatar newValue && newValue != vm.Avatar_Preset)
            {

                //
                //
                vm.Avatar_Preset = newValue;
                vm.Avatar        = vm.GetAvatar(newValue);

                //
                //
                Xplat.Debug($"用户修改了默认的头像，新值为:{newValue}");

                //
                //
                await Platform.SaveSetting(vm.Setting_App);

                //
                //
                UpdateButtonState();

            }
        }
    }
}